﻿using System;
using System.Activities;
using System.Collections.Generic;
using KPIS.GERP.SERVICE.Model;
using KPIS.GERP.SERVICE.WorkflowService.Class;

namespace KPIS.GERP.SERVICE.WorkflowService.CodeActivity
{
    #region RuleAbsence
    public sealed class Select_RuleAbsence : CodeActivity<List<rule_absence>>
    {
        public InArgument<string> in_rule_absence_seq { get; set; }
        public InArgument<string> in_emp_type_seq { get; set; }
        public InArgument<string> in_absence_type_seq { get; set; }

        /// <summary>
        /// set True for join this object
        /// </summary>
        public InArgument<bool> in_isGetHrmPsEmployeeType { get; set; }
        /// <summary>
        /// set True for join this object
        /// </summary>
        public InArgument<bool> in_isGetHrmPsAbsenceType { get; set; }
        /// <summary>
        /// set True for join this object
        /// </summary>
        public InArgument<bool> in_isGetRuleApproveType { get; set; }

        protected override List<rule_absence> Execute(CodeActivityContext context)
        {
            List<rule_absence> list = new DAL.RuleAbsence().GetRuleAbsence(
                in_rule_absence_seq.Get(context)
                , in_emp_type_seq.Get(context)
                , in_absence_type_seq.Get(context)
                , in_isGetHrmPsEmployeeType.Get(context)
                , in_isGetHrmPsAbsenceType.Get(context)
                , in_isGetRuleApproveType.Get(context)
                );
            return list;
        }
    }

    public sealed class Insert_RuleAbsence : CodeActivity<string>
    {
        public InArgument<string> in_emp_type_seq { get; set; }
        public InArgument<string> in_absence_type_seq { get; set; }
        public InArgument<string> in_send_before { get; set; }
        public InArgument<string> in_include_nwd { get; set; }
        public InArgument<string> in_day_limit { get; set; }
        public InArgument<string> in_day_limit_full_salary { get; set; }
        public InArgument<string> in_day_limit_half_salary { get; set; }
        public InArgument<string> in_time_limit { get; set; }
        public InArgument<string> in_day_per_time_limit { get; set; }
        public InArgument<string> in_remarks { get; set; }
        public InArgument<string> in_record_status { get; set; }
        public InArgument<string> in_created_by { get; set; }

        protected override string Execute(CodeActivityContext context)
        {
            return new DAL.RuleAbsence().InsertRuleAbsence(
                in_emp_type_seq.Get(context)
                , in_absence_type_seq.Get(context)
                , in_send_before.Get(context)
                , in_include_nwd.Get(context)
                , in_day_limit.Get(context)
                , in_day_limit_full_salary.Get(context)
                , in_day_limit_half_salary.Get(context)
                , in_time_limit.Get(context)
                , in_day_per_time_limit.Get(context)
                , in_remarks.Get(context)
                , in_record_status.Get(context)
                , in_created_by.Get(context));
        }
    }

    public sealed class Update_RuleAbsence : System.Activities.CodeActivity
    {
        [RequiredArgument]
        public InArgument<string> in_rule_absence_seq { get; set; }
        public InArgument<string> in_emp_type_seq { get; set; }
        public InArgument<string> in_absence_type_seq { get; set; }
        public InArgument<string> in_send_before { get; set; }
        public InArgument<string> in_include_nwd { get; set; }
        public InArgument<string> in_day_limit { get; set; }
        public InArgument<string> in_day_limit_full_salary { get; set; }
        public InArgument<string> in_day_limit_half_salary { get; set; }
        public InArgument<string> in_time_limit { get; set; }
        public InArgument<string> in_day_per_time_limit { get; set; }
        public InArgument<string> in_remarks { get; set; }
        public InArgument<string> in_record_status { get; set; }
        public InArgument<string> in_updated_by { get; set; }

        protected override void Execute(CodeActivityContext context)
        {
            new DAL.RuleAbsence().UpdateRuleAbsence(
                in_rule_absence_seq.Get(context)
                , in_emp_type_seq.Get(context)
                , in_absence_type_seq.Get(context)
                , in_send_before.Get(context)
                , in_include_nwd.Get(context)
                , in_day_limit.Get(context)
                , in_day_limit_full_salary.Get(context)
                , in_day_limit_half_salary.Get(context)
                , in_time_limit.Get(context)
                , in_day_per_time_limit.Get(context)
                , in_remarks.Get(context)
                , in_record_status.Get(context)
                , in_updated_by.Get(context));
        }
    }

    public sealed class UpdateStatus_RuleAbsence : System.Activities.CodeActivity
    {
        [RequiredArgument]
        public InArgument<string> in_rule_absence_seq { get; set; }

        [RequiredArgument]
        public InArgument<string> in_record_status { get; set; }
        public InArgument<string> in_updated_by { get; set; }

        protected override void Execute(CodeActivityContext context)
        {
            new DAL.RuleAbsence().UpdateStatusRuleAbsence(
                in_rule_absence_seq.Get(context)
                , in_record_status.Get(context)
                , in_updated_by.Get(context));
        }
    }
    #endregion

    #region RuleApprove
    public sealed class Select_RuleApprove : CodeActivity<List<rule_approve>>
    {
        /// <summary>
        /// each rule
        /// </summary>
        public InArgument<string> in_rule_approve_seq { get; set; }

        /// <summary>
        /// group of rule
        /// </summary>
        public InArgument<string> in_rule_approve_type_seq { get; set; }

        /// <summary>
        /// set True for join this object
        /// </summary>
        public InArgument<bool> in_IsGetRuleApproveType { get; set; }
        /// <summary>
        /// set True for join this object
        /// </summary>
        public InArgument<bool> in_IsGetRuleApproveGroup { get; set; }
        /// <summary>
        /// set True for join this object
        /// </summary>
        public InArgument<bool> in_IsGetApprover { get; set; }
        /// <summary>
        /// set True for join this object
        /// </summary>
        public InArgument<int?> in_Step { get; set; }

        protected override List<rule_approve> Execute(CodeActivityContext context)
        {
            List<rule_approve> list = new DAL.RuleApprove().GetRuleApprove(
                in_rule_approve_seq.Get(context)
                , in_rule_approve_type_seq.Get(context)
                , in_IsGetRuleApproveType.Get(context)
                , in_IsGetRuleApproveGroup.Get(context)
                , in_IsGetApprover.Get(context) 
                , in_Step.Get(context)
                );
            return list;
        }
    }

    public sealed class Insert_RuleApprove : CodeActivity<string>
    {
        public InArgument<string> in_rule_approve_type_seq { get; set; }
        public InArgument<string> in_step { get; set; }
        public InArgument<string> in_approver_type { get; set; }
        public InArgument<string> in_approver_seq { get; set; }
        public InArgument<string> in_action { get; set; }
        public InArgument<string> in_remarks { get; set; }
        public InArgument<string> in_record_status { get; set; }
        public InArgument<string> in_created_by { get; set; }

        protected override string Execute(CodeActivityContext context)
        {
            return new DAL.RuleApprove().InsertRuleApprove(
                in_rule_approve_type_seq.Get(context)
                , in_step.Get(context)
                , in_approver_type.Get(context)
                , in_approver_seq.Get(context)
                , in_action.Get(context)
                , in_remarks.Get(context)
                , in_record_status.Get(context)
                , in_created_by.Get(context));
        }
    }

    public sealed class Update_RuleApprove : System.Activities.CodeActivity
    {
        [RequiredArgument]
        public InArgument<string> in_rule_approve_seq { get; set; }
        public InArgument<string> in_rule_approve_type_seq { get; set; }
        public InArgument<string> in_step { get; set; }
        public InArgument<string> in_approver_type { get; set; }
        public InArgument<string> in_approver_seq { get; set; }
        public InArgument<string> in_action { get; set; }
        public InArgument<string> in_remarks { get; set; }
        public InArgument<string> in_record_status { get; set; }
        public InArgument<string> in_updated_by { get; set; }

        protected override void Execute(CodeActivityContext context)
        {
            new DAL.RuleApprove().UpdateRuleApprove(
                in_rule_approve_seq.Get(context)
                , in_rule_approve_type_seq.Get(context)
                , in_step.Get(context)
                , in_approver_type.Get(context)
                , in_approver_seq.Get(context)
                , in_action.Get(context)
                , in_remarks.Get(context)
                , in_record_status.Get(context)
                , in_updated_by.Get(context));
        }
    }

    public sealed class UpdateStatus_RuleApprove : System.Activities.CodeActivity
    {
        [RequiredArgument]
        public InArgument<string> in_rule_approve_seq { get; set; }

        [RequiredArgument]
        public InArgument<string> in_record_status { get; set; }
        public InArgument<string> in_updated_by { get; set; }

        protected override void Execute(CodeActivityContext context)
        {
            new DAL.RuleApprove().UpdateStatusRuleApprove(
                in_rule_approve_seq.Get(context)
                , in_record_status.Get(context)
                , in_updated_by.Get(context));
        }
    }
    #endregion

    #region RuleApproveType
    public sealed class Select_RuleApproveType : CodeActivity<List<rule_approve_type>>
    {
        public InArgument<string> in_rule_approve_type_seq { get; set; }
        public InArgument<string> in_rule_approve_group_seq { get; set; }

        /// <summary>
        /// set True for join this object
        /// </summary>
        public InArgument<bool> in_IsGetRuleApproveGroup { get; set; }

        protected override List<rule_approve_type> Execute(CodeActivityContext context)
        {
            List<rule_approve_type> list = new DAL.RuleApprove().GetRuleApproveType(
                in_rule_approve_type_seq.Get(context)
                , in_rule_approve_group_seq.Get(context)
                , in_IsGetRuleApproveGroup.Get(context)
                );
            return list;
        }
    }

    public sealed class Insert_RuleApproveType : CodeActivity<string>
    {
        public InArgument<string> in_rule_approve_group_seq { get; set; }
        public InArgument<string> in_rule_approve_type_name { get; set; }
        public InArgument<string> in_remarks { get; set; }
        public InArgument<string> in_record_status { get; set; }
        public InArgument<string> in_created_by { get; set; }

        protected override string Execute(CodeActivityContext context)
        {
            return new DAL.RuleApprove().InsertRuleApproveType(
                in_rule_approve_group_seq.Get(context)
                , in_rule_approve_type_name.Get(context)
                , in_remarks.Get(context)
                , in_record_status.Get(context)
                , in_created_by.Get(context));
        }
    }

    public sealed class Update_RuleApproveType : System.Activities.CodeActivity
    {
        [RequiredArgument]
        public InArgument<string> in_rule_approve_type_seq { get; set; }
        public InArgument<string> in_rule_approve_group_seq { get; set; }
        public InArgument<string> in_rule_approve_type_name { get; set; }
        public InArgument<string> in_remarks { get; set; }
        public InArgument<string> in_record_status { get; set; }
        public InArgument<string> in_updated_by { get; set; }

        protected override void Execute(CodeActivityContext context)
        {
            new DAL.RuleApprove().UpdateRuleApproveType(
                in_rule_approve_type_seq.Get(context)
                , in_rule_approve_group_seq.Get(context)
                , in_rule_approve_type_name.Get(context)
                , in_remarks.Get(context)
                , in_record_status.Get(context)
                , in_updated_by.Get(context));
        }
    }

    public sealed class UpdateStatus_RuleApproveType : System.Activities.CodeActivity
    {
        [RequiredArgument]
        public InArgument<string> in_rule_approve_type_seq { get; set; }

        [RequiredArgument]
        public InArgument<string> in_record_status { get; set; }
        public InArgument<string> in_updated_by { get; set; }

        protected override void Execute(CodeActivityContext context)
        {
            new DAL.RuleApprove().UpdateStatusRuleApproveType(
                in_rule_approve_type_seq.Get(context)
                , in_record_status.Get(context)
                , in_updated_by.Get(context));
        }
    }
    #endregion

    #region RuleApproveGroup
    public sealed class Select_RuleApproveGroup : CodeActivity<List<rule_approve_group>>
    {
        public InArgument<string> in_rule_approve_group_seq { get; set; }

        protected override List<rule_approve_group> Execute(CodeActivityContext context)
        {
            List<rule_approve_group> list = new DAL.RuleApprove().GetRuleApproveGroup(
                in_rule_approve_group_seq.Get(context)
                );
            return list;
        }
    }

    public sealed class Insert_RuleApproveGroup : CodeActivity<string>
    {
        public InArgument<string> in_rule_approve_group_name { get; set; }
        public InArgument<string> in_remarks { get; set; }
        public InArgument<string> in_record_status { get; set; }
        public InArgument<string> in_created_by { get; set; }

        protected override string Execute(CodeActivityContext context)
        {
            return new DAL.RuleApprove().InsertRuleApproveGroup(
                in_rule_approve_group_name.Get(context)
                , in_remarks.Get(context)
                , in_record_status.Get(context)
                , in_created_by.Get(context));
        }
    }

    public sealed class Update_RuleApproveGroup : System.Activities.CodeActivity
    {
        [RequiredArgument]
        public InArgument<string> in_rule_approve_group_seq { get; set; }
        public InArgument<string> in_rule_approve_group_name { get; set; }
        public InArgument<string> in_remarks { get; set; }
        public InArgument<string> in_record_status { get; set; }
        public InArgument<string> in_updated_by { get; set; }

        protected override void Execute(CodeActivityContext context)
        {
            new DAL.RuleApprove().UpdateRuleApproveGroup(
                in_rule_approve_group_seq.Get(context)
                , in_rule_approve_group_name.Get(context)
                , in_remarks.Get(context)
                , in_record_status.Get(context)
                , in_updated_by.Get(context));
        }
    }

    public sealed class UpdateStatus_RuleApproveGroup : System.Activities.CodeActivity
    {
        [RequiredArgument]
        public InArgument<string> in_rule_approve_group_seq { get; set; }

        [RequiredArgument]
        public InArgument<string> in_record_status { get; set; }
        public InArgument<string> in_updated_by { get; set; }

        protected override void Execute(CodeActivityContext context)
        {
            new DAL.RuleApprove().UpdateStatusRuleApproveGroup(
                in_rule_approve_group_seq.Get(context)
                , in_record_status.Get(context)
                , in_updated_by.Get(context));
        }
    }
    #endregion

#region[Pe]
    //ตรวจสอบลาก่อน ลาหลัง//
    public sealed class Rule_BeforAfter : CodeActivity<Boolean>
    {
        public InArgument<DateTime> in_date_request { get; set; }
        public InArgument<int> in_date_rule { get; set; }
        public InArgument<DateTime> in_date_leave { get; set; }
        Boolean ret = false;
        protected override Boolean Execute(CodeActivityContext context)
        {
            Utility u = new Utility();
            int workingday = u.GetRegularWorkingDays(in_date_request.Get(context), in_date_leave.Get(context));
            int everyday = u.GetRegularAllDays(in_date_request.Get(context), in_date_leave.Get(context)) - 1;
            int addday = workingday - everyday;
            addday += in_date_rule.Get(context);
            if (in_date_request.Get(context).AddDays(addday) <= in_date_leave.Get(context))
            {
                ret = true;
            }
            return ret;
        }
    }
    //นับวันลา ไม่รวมเสาร์ อาทิตย์และวันหยุด//
    public sealed class Rule_CountDay : CodeActivity<Decimal>
    {
        public InArgument<DateTime> in_date_from { get; set; }
        public InArgument<DateTime> in_date_to { get; set; }
        public InArgument<string> in_time_from { get; set; }
        public InArgument<string> in_time_to { get; set; }

        protected override Decimal Execute(CodeActivityContext context)
        {
            Decimal n = 0;
            if (in_date_from.Get(context).Date.Equals(in_date_to.Get(context).Date))
            {
                Utility u = new Utility();
                n = u.GetRegularWorkingDays(in_date_from.Get(context).Date, in_date_from.Get(context).Date);
                if (n >= 1)
                {                  
                    if (in_time_from.Get(context).Equals("M") && in_time_to.Get(context).Equals("A")) n = 1;
                    else if (in_time_from.Get(context).Equals("A") && in_time_to.Get(context).Equals("A")) n = (Decimal)0.5;
                    else if (in_time_from.Get(context).Equals("M") && in_time_to.Get(context).Equals("M")) n = (Decimal)0.5;
                    else n = 0;
                }
            }
            else if (in_date_from.Get(context).Date < in_date_to.Get(context).Date)
            {
                Utility u = new Utility();
                n = u.GetRegularWorkingDays(in_date_from.Get(context).Date, in_date_to.Get(context).Date);
                if (n >= 1)
                {
                    if (in_time_from.Get(context).Equals("M") && in_time_to.Get(context).Equals("A")) n -= 0;
                    else if (in_time_from.Get(context).Equals("A") && in_time_to.Get(context).Equals("A")) n -= (Decimal)0.5;
                    else if (in_time_from.Get(context).Equals("M") && in_time_to.Get(context).Equals("M")) n -= (Decimal)0.5;
                    else if (in_time_from.Get(context).Equals("A") && in_time_to.Get(context).Equals("M")) n -= 1;
                    else n = 0;
                }
            }
            else {
                n = 0;
            }         
            return n;
        }
    }

    public sealed class Rule_Gender : CodeActivity<bool>
    {
        public InArgument<string> in_gender_seq { get; set; }
        public InArgument<string> in_absence_type_seq { get; set; }

        protected override bool Execute(CodeActivityContext context)
        {
            //in_gender_seq male=1,female=2
            //in_absence_type_seq 6=ทหาร, 16=เตรียมความพร้อมทหาร, 2=ลาคลอด
            bool ret = false;
            if (in_absence_type_seq.Get(context).Equals("6") || in_absence_type_seq.Get(context).Equals("16"))
            {
                if (in_gender_seq.Get(context).Equals("1"))
                {
                    ret = true;
                }
            }
            else if (in_absence_type_seq.Get(context).Equals("2"))
            {
                if (in_gender_seq.Get(context).Equals("2"))
                {
                    ret = true;
                }
            }
            else {
                ret = true;
            }

            return ret;
        }
    }

#endregion[Pe]
}
